package mysql

import (
	"goods/goodsrpc/goodsrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null"`
	Price   float64 `gorm:"type:decimal(10,2)"`
	Stock   int64   `gorm:"type:int(11)"`
	Image   string  `gorm:"type:varchar(30);not null"`
	ClassId int64   `gorm:"type:int(11)"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(g).Error
}
func GoodsList(db *gorm.DB, list []*goodsrpc.GoodsList) []*goodsrpc.GoodsList {
	db.Debug().Model(&Goods{}).
		Select("goods.id as goods_id,goods.name as good_name,goods.price,goods.stock,goods.image,classes.name as class_name,goods.created_at as create_ad").
		Joins("JOIN classes ON goods.class_id = classes.id").
		Find(&list)
	return list
}
